package turtle.system.pt;

import java.util.ArrayList;

public class StatCalc {

	private String desc = null;
	private int count; // Number of numbers that have been entered.
	private double sum; // The sum of all the items that have been entered.
	private double squareSum; // The sum of the squares of all the items.
	private double max = -10000000;
	private double min = 10000000;
	private int positiveCount;
	private int negativeCount;
	private double positiveSum;
	private double negativeSum;
	private int[] maxParam;
	private int[] minParam;
	public MDD maxDrawDown = new MDD();
	public ArrayList<Double> valueList = new ArrayList<Double>();

	public StatCalc(String description) {
		this.desc = description;
	}
	public StatCalc() {
		this.desc = null;
	}
	public void enter(double num, Object param) {
		enter(num);
		maxDrawDown.insertData(num);
		valueList.add(new Double(num));
		if (!(param instanceof int[])) {
			throw new RuntimeException("param in not of type int[]");
		}
		int[] strategy = (int[]) param;
		if (max == num) { // 在 enter(double) 中已比較變更過 max
			maxParam = strategy;
		}
		if (min == num) { // 在 enter(double) 中已比較變更過 min
			minParam = strategy;
		}
	}

	public int[] getMaxParam() {
		return maxParam;
	}

	public int[] getMinParam() {
		return minParam;
	}

	public double getLeastNum() {
		return valueList.get(valueList.size() - 1);
	}

	public ArrayList<Double> getHistoryList() {
		ArrayList<Double> ret = new ArrayList<Double>();
		double history = 0;
		for (int i=0; i<this.valueList.size();i++) {
			history +=this.valueList.get(i);
			ret.add(new Double(history));
		}
		return ret;
	}
	public void enter(double num) {
		// Add the number to the dataset.

		maxDrawDown.insertData(num);
		valueList.add(new Double(num));
		count++;
		sum += num;
		squareSum += num * num;
		max = Math.max(max, num);
		min = Math.min(min, num);
		if (num > 0) {
			positiveCount++;
			positiveSum += num;
		} else {
			negativeCount++;
			negativeSum += num;
		}
	}

	public int getPositiveCount() {
		return positiveCount;
	}

	public int getNegativeCount() {
		return negativeCount;
	}

	public double getPositiveMean() {
		return positiveSum / positiveCount;
	}

	public double getNegativeMean() {
		return negativeSum / negativeCount;
	}

	public int getCount() {
		// Return number of items that have been entered.
		return count;
	}

	public double getSum() {
		// Return the sum of all the items that have been entered.
		return sum;
	}

	public double getMean() {
		// Return average of all the items that have been entered.
		// Value is Double.NaN if count == 0.
		return sum / count;
	}

	public double getTruncatedMean() {
		return (sum - max - min) / (count - 2);
	}

	public double getStandardDeviation() {
		// Return standard deviation of all the items that have been entered.
		// Value will be Double.NaN if count == 0.
		double mean = getMean();
		return Math.sqrt(squareSum / count - mean * mean);
	}

	public double getMax() {
		return max;
	}

	public double getMin() {
		return min;
	}
	public String toString() {
		String ret = desc+":"+(String
				.format(
						"總獲利%.0f ,共%d筆交易(%dW/%dL,勝率%.3f), ave每筆交易獲利=%.1f(%.1fW/%.1fL),去頭尾平均=%.1f,max單筆獲利=%.0f,min單筆獲利=%.0f,單筆獲利std=%.1f",
						this.getSum(),
						this.getCount(),
						this.getPositiveCount(), this.getNegativeCount(),
						(this.getPositiveCount() + 0.0)/ this.getCount(),
						this.getMean(),
						this.getPositiveMean(),this.getNegativeMean(),
					    this.getTruncatedMean(),
					    this.getMax(),
					    this.getMin(),
					    this.getStandardDeviation()));
		return ret;
	}

} // end of class StatCalc